package cn.com.yto56.coresystem.business.msg.soa.biz.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IMsgTempletBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IMsgTempletDao;
import cn.com.yto56.coresystem.logic.msg.soa.domain.MsgTempletBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangrui on 2017/2/17.
 */
@Service
public class MsgTempletBizImpl implements IMsgTempletBiz {
    @Autowired
    private IMsgTempletDao msgTempletDaoImpl;

    @Override
    public AppResult addMsgTemplet(String sysInfo_Id, String templetInfo, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(templetInfo)) {
            resultMessage = "参数[yto_msg_yto_msg_templetInfo]不能为空";
        } else {
            MsgTempletBean msgTempletBean = new MsgTempletBean();
            msgTempletBean.setYto_msg_sysinfo_id(sysInfo_Id);
            msgTempletBean.setYto_msg_templetinfo(templetInfo);
            msgTempletBean.setYto_msg_sys_remark(sys_remark);
            msgTempletBean.setCreate_time(new Date());
            String msgtempletId = "MSG_TPL_" + new Date().getTime();
            msgTempletBean.setYto_msg_msgtempletid(msgtempletId);
            int t = msgTempletDaoImpl.add(msgTempletBean);
            if (t <= 0) {
                resultMessage = "[系统异常]sql执行失败";
            } else {
                datas = new HashMap();
                datas.put("yto_msg_msgTempletId", msgtempletId);
                resultFlag = true;
                resultMessage = "执行成功";
            }

        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    @Transactional
    public AppResult deleteMsgTemplet(String sysInfo_Id, String msgTempletId, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(msgTempletId)) {
            resultMessage = "参数[yto_msg_msgTempletId]不能为空";
        } else {
            MsgTempletBean msgTempletBean = new MsgTempletBean();
            msgTempletBean.setYto_msg_sysinfo_id(sysInfo_Id);
            msgTempletBean.setYto_msg_msgtempletid(msgTempletId);
            msgTempletBean.setStatus("VALID");
            boolean checkUpdate = msgTempletDaoImpl.searchForUpdate(msgTempletBean);
            if (checkUpdate) {
                resultMessage = "不存在模板为[" + msgTempletId + "]或已经被删除";
            } else {
                msgTempletBean.setYto_msg_sys_remark(sys_remark);
                int t = msgTempletDaoImpl.delete(msgTempletBean);
                if (t <= 0) {
                    resultMessage = "[系统异常]sql执行失败";
                } else {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult updateMsgTemplet(String sysInfo_Id, String msgTempletId, String templetInfo, String sys_remark) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(msgTempletId)) {
            resultMessage = "参数[yto_msg_msgTempletId]不能为空";
        } else if (StringUtils.isBlank(templetInfo)) {
            resultMessage = "参数[yto_msg_templetInfo]不能为空";
        } else {
            MsgTempletBean set = new MsgTempletBean();
            set.setYto_msg_templetinfo(templetInfo);
            set.setYto_msg_sys_remark(sys_remark);
            set.setUpdate_time(new Date());
            MsgTempletBean where = new MsgTempletBean();
            where.setYto_msg_sysinfo_id(sysInfo_Id);
            where.setYto_msg_msgtempletid(msgTempletId);
            int t = msgTempletDaoImpl.update(set, where);
            if (t <= 0) {
                resultMessage = "[系统异常]sql执行失败";
            } else {
                resultFlag = true;
                resultMessage = "执行成功";
            }
        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }
}
